package adminLoginPack;

import java.io.File;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;



public class AdminInsertUpdateAction extends Action {


	  public ActionForward execute(ActionMapping mapping, ActionForm inForm, HttpServletRequest request, HttpServletResponse response) throws Exception {
		 
		  Connection con=null;
		  Statement st=null;
		  try{
			  
			  //Starts:
			  //inForm.get
			  //Ends.
			  
			  String id = "";
			  ResultSet rs = null;
			  int isUpdate = 0;
			  
			  request.getParameter("file");
			  
			    Class.forName("org.gjt.mm.mysql.Driver");
			    con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root1");
				st=con.createStatement();
				
			    File fXmlFile = new File("C:/Users/shriya/Desktop/dateImport@02092013/Circulars.xml");
				DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
				DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
				Document doc = dBuilder.parse(fXmlFile);
				doc.getDocumentElement().normalize();
			 
				NodeList nList1 = doc.getElementsByTagName("investment-circular-insert");
				
				Node nNode1 = nList1.item(0);
				NamedNodeMap map1 = nNode1.getAttributes();
				Node ntempNode1 = map1.getNamedItem("name");
				
				String nameOfTheTable = ntempNode1.getNodeValue();
				id = nameOfTheTable;
				
				System.out.println("nnnn'ddd");
				
				NodeList nList = doc.getElementsByTagName("table");
				if("Circulars".equals(id)){
					
					st.execute("delete from circulars");
									 
						for (int temp = 0; temp < nList.getLength(); temp++) {
					 
							Node nNode = nList.item(temp);
							NamedNodeMap map = nNode.getAttributes();
							Node ntempNode = map.getNamedItem("name");
						if (nNode.getNodeType() == Node.ELEMENT_NODE) {
							 
								Element eElement = (Element) nNode;
								
								String serviceno = eElement.getElementsByTagName("service_no").item(0).getTextContent();
								String circular_number = eElement.getElementsByTagName("circular_number").item(0).getTextContent();
								String circular_subject = eElement.getElementsByTagName("circular_subject").item(0).getTextContent();
								String created_by = eElement.getElementsByTagName("created_by").item(0).getTextContent();
								String created_on = eElement.getElementsByTagName("created_on").item(0).getTextContent();
								String master = eElement.getElementsByTagName("master").item(0).getTextContent();
								String Date_of_circular = eElement.getElementsByTagName("Date_of_circular").item(0).getTextContent();
					 
								//DELETE OLD DATA
								
								
								//ENTER NEW DATA.
								
								/*if(circular_subject.contains("'")){
									circular_subject.replace("'", "/'");
								}*/
								
								rs = st.executeQuery("select max(seq_no) from circulars");
								
								BigDecimal seq_no = null;
								while(rs.next()){
									seq_no = rs.getBigDecimal(1);
								}
								
								if(null == seq_no){
									seq_no = new BigDecimal("1");
								}else{
									
									seq_no = new BigDecimal(seq_no.intValue()+1);
								}
								
								st.executeUpdate("insert  into circulars(seq_no,`Service_no`,`TOPIC`," +
										"`circular_subject`,`Created_By`,`Created_On`,`master`,`date_of_circular`)"+
										" values" + 
										" ("+seq_no+","+serviceno+",'"+circular_number+"','"+circular_subject+"','"+created_by+"','"+created_on+"','"+master+"','"+Date_of_circular+"')");
				 
						}
					}
					
				}else if("Articles".equals(id)){

					 String ArticlesNumber = request.getParameter("ArticlesNumber");
					 String articlesSubjectMatter = request.getParameter("articlesSubjectMatter");
					 
					 String DOA = request.getParameter("DOA");
					 
					 //String dateofNotification1 = DateFormatTest(DOA);
					 

					/*isUpdate = st.executeUpdate("insert  into `articles`(`Service_no`,`articlesSubjectMatter`,`CreatedOn`,`ArticlesNumber`" +
									",`date_of_article`,`CreatedBy`)"+
									" values" + 
									" ('"+Services+"','"+articlesSubjectMatter+"','"+mysqlDateString+"','"+ArticlesNumber+"','"+dateofNotification1+"','Admin')");
*/					
				}else if("Notifications".equals(id)){
					
					//DELETE OLD DATA
					st.execute("delete from notification");
					
					for (int temp = 0; temp < nList.getLength(); temp++) {
						 
						Node nNode = nList.item(temp);
						NamedNodeMap map = nNode.getAttributes();
						Node ntempNode = map.getNamedItem("name");
					if (nNode.getNodeType() == Node.ELEMENT_NODE) {
						 
							Element eElement = (Element) nNode;
							
							String serviceno = eElement.getElementsByTagName("service_no").item(0).getTextContent();
							String Notification_Number = eElement.getElementsByTagName("Notification_Number").item(0).getTextContent();
							String Date_of_Notification = eElement.getElementsByTagName("Date_of_Notification").item(0).getTextContent();
							String Subject_Matter = eElement.getElementsByTagName("Subject_Matter").item(0).getTextContent();
							String Effective_date = eElement.getElementsByTagName("Effective_date").item(0).getTextContent();
							String createdBy = eElement.getElementsByTagName("createdBy").item(0).getTextContent();
							String createdOn = eElement.getElementsByTagName("createdOn").item(0).getTextContent();
				 
							//ENTER NEW DATA
							isUpdate = st.executeUpdate("insert  into notification(`serviceno`," +
							"`Notification_Number`,`Date_of_Notification`,`Subject_Matter`,"+
							"`Effective_date`,`createdBy`,`createdon`)"+
							" values" + 
							" ("+serviceno+",'"+Notification_Number+"','"+Date_of_Notification+"','"+Subject_Matter+"','"+Effective_date+"','"+createdBy+"','"+createdOn+"')");
					 		
					}
				}

				}else if("PressNotes".equals(id)){

					 /* String Slnumber = null != request.getParameter("Slnumber")?(String)request.getParameter("Slnumber"):"";
					  String Subject = null != request.getParameter("Subject")?(String)request.getParameter("Subject"):"";
					  String Notes = null != request.getParameter("Notes")?request.getParameter("Notes"):"";
					  String date1 = null != request.getParameter("Date1")?request.getParameter("Date1"):"";
					  
					  
					  String dateofNotification1 = DateFormatTest(date1);
					  String EFD = DateFormatTest(null != request.getParameter("EFD")?request.getParameter("EFD"):"");*/
					  
					/*isUpdate =st.executeUpdate("insert  into `pressnotes`(`ServiceNum`,`Slumber`,`Subject`,`Notes`,`CreatedBy`,`CreatedOn`,`Normal_date`,`Effective_date`) "+
											" values" + 
											" ("+Services+","+Slnumber+",'"+Subject+"','"+Notes+"','admin','"+mysqlDateString+"','"+dateofNotification1+"','"+EFD+"')");
					*/
				}else if("faq".equals(id)){

					st.execute("delete from FAQ");
					
					for (int temp = 0; temp < nList.getLength(); temp++) {
						 
						Node nNode = nList.item(temp);
						NamedNodeMap map = nNode.getAttributes();
						Node ntempNode = map.getNamedItem("name");
					if (nNode.getNodeType() == Node.ELEMENT_NODE) {
						 
							Element eElement = (Element) nNode;
							
							String serviceno = eElement.getElementsByTagName("service_no").item(0).getTextContent();
							String Question = eElement.getElementsByTagName("Question").item(0).getTextContent();
							String Answer = eElement.getElementsByTagName("Answer").item(0).getTextContent();
							String created_by = eElement.getElementsByTagName("CreatedBy").item(0).getTextContent();
							String created_on = eElement.getElementsByTagName("CreatedOn").item(0).getTextContent();

							
							rs = st.executeQuery("select max(QuestionNum) from FAQ");
							
							BigDecimal questionNume = null;
							while(rs.next()){
								questionNume = rs.getBigDecimal(1);
							}
							
							if(null == questionNume){
								questionNume = new BigDecimal("1");
							}else{
								
								questionNume = new BigDecimal(questionNume.intValue()+1);
							}
							
							String question = request.getParameter("Question");
							String answer = request.getParameter("Answer");
							  
							isUpdate =st.executeUpdate("insert  into FAQ(QuestionNum,`Question`," +
											"`Answer`,`CreatedBy`,`CreatedOn`,ServiveNum)"+
											" values" + 
											" ("+questionNume+",'"+Question+"','"+Answer+"','"+created_by+"','"+created_on+"',"+serviceno+")");
							
					}
				}
					
					
				}else if("Definitions".equals(id)){
					
					
					 String RNo = null != request.getParameter("RNo")?(String)request.getParameter("RNo"):"";
					  String ARR = null != request.getParameter("ARR")?(String)request.getParameter("ARR"):"";
					  String caption = null != request.getParameter("caption")?request.getParameter("caption"):"";
					  String Definition = null != request.getParameter("Definition")?request.getParameter("Definition"):"";
					  
					  
					 
						
						
						/*isUpdate = st.executeUpdate("insert  into Definitions(`Service_no`,`Section_Rule`," +
								"`act_rule`,`Caption`,`definition`,`created_by`,`created_on`)"+
								" values" + 
								" ("+Services+",'"+RNo+"','"+ARR+"','"+caption+"','"+Definition+"','admin','"+mysqlDateString+"')");
						
						 if("Y".equals(masterOrNot)){
							  id="Master Circular";
						  }*/
						
					}
				
				//}
				
		  }catch(Exception e){
			  e.printStackTrace();
		  }finally{
			  if(null !=st)st.close();
			  if(null !=con)con.close();
		  }
		   
		  return null;
	    
	  }
	  
}
